﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    /// <summary>
    /// Determines the logic behind button clicks
    /// </summary>
    public enum Button_Type
    {
        //Title Menu
        TitleCampaignStart,
        TitleArenaStat,
        TitleOptions,
        TitleExitGame,

        //Arena
        ArenaToTitle,

        //Title Modar

        //Campaign Select
        CampaignSelectSave,
        CampaignSelectLoad,
        CampaignSelectRandom,
        CampaignSelectBack,
        CampaignSelectBegin,

        //Character Edit
        EditBack,

        //HACK
        Multi,


    }

    /// <summary>
    /// Determines button look and feel
    /// </summary>
    public enum Button_Style
    {
        Basic
    }

    /// <summary>
    /// Determines button look and feel
    /// </summary>
    public enum ButtonBehaviour
    {
        Regular,
        Radio
    }

    class ButtonControl : UserControl
    {
        #region Fields

        protected SpriteFont font;

        protected GameStateMessage clickMessage;

        protected ButtonBehaviour behaviour = ButtonBehaviour.Regular;
        
        /// <summary>
        /// For radio style buttons
        /// </summary>
        protected bool pressed = false;

        protected Button_Style _style;
        public Button_Style style
        {
            get { return _style; }
            set
            {
                _style = value;
                switch (value)
                {
                    case Button_Style.Basic:
                        bgTexture = TacticsGame.Instance.Content.Load<Texture2D>("Menus/Buttons/Button");
                        break;
                }
            }
        }

        protected Button_Type _type;
        public Button_Type type
        {
            get { return _type; }
            set
            {
                _type = value;
                switch (value)
                {
#region Title Buttons
                    case Button_Type.TitleArenaStat:
                        message = "Arena";   
                        clickMessage = GameStateMessage.ArenaMenu;                     
                        break;
                    case Button_Type.TitleExitGame:
                        message = "Quit Game";
                        clickMessage = GameStateMessage.Quit;
                        break;
                    case Button_Type.TitleOptions:
                        message = "Options";                        
                        break;
                    case Button_Type.TitleCampaignStart:
                        message = "Campaign";
                        clickMessage = GameStateMessage.CampaignMenu;
                        break;

                    //Arena Buttons
                    case Button_Type.ArenaToTitle:
                        message = "Back to Future";
                        clickMessage = GameStateMessage.Title;
                        width = 125;
                        break;
#endregion

#region Campaign Select
                    case Button_Type.CampaignSelectBack:
                        message = "Back";
                        clickMessage = GameStateMessage.Title;
                        break;
                    case Button_Type.CampaignSelectBegin:
                        message = "Begin";
                        clickMessage = GameStateMessage.ArenaMenu;
                        break;
                    case Button_Type.CampaignSelectLoad:
                        message = "Load Team";
                        break;
                    case Button_Type.CampaignSelectRandom:
                        message = "Random Team";
                        behaviour = ButtonBehaviour.Radio;
                        break;
                    case Button_Type.CampaignSelectSave:
                        message = "Save Team";
                        break;
#endregion

                    #region Character Edit
                    case Button_Type.EditBack:
                        message = "Back";
                        clickMessage = GameStateMessage.CampaignMenu;
                        break;
                    #endregion

                    case Button_Type.Multi:
                        message = "MULTIPLAYER";
                        clickMessage = GameStateMessage.MultiplayerOn;
                        width = 125;
                        break;


                }
            }
        }

        protected string message;

        #endregion

        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureButtonRegular);
            font = TacticsGame.Instance.gameFont;

            width = Constants.ButtonWidth;
            height = Constants.ButtonHeight;
            depth = ControlDepth.NonDrag;
        }

        /// <summary>
        /// Updates logic based on mouse
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            clickHandle();

        }

        public virtual void clickHandle()
       {
             if (behaviour == ButtonBehaviour.Regular)
            {
                if (InputMgr.Instance.isMouseClickUp(Click.Left) && Hover)
                {
                    GameLogic.Instance.gameStateLogic.state = clickMessage;
                }
            }
            else if (behaviour == ButtonBehaviour.Radio)
            {
                if (InputMgr.Instance.isMouseClickUp(Click.Left) && Hover)
                {
                    pressed = !pressed;
                }
            }
       }

        /// <summary>
        /// Draws button based on location
        /// </summary>
        /// <param name="gameTime">gameTime since last draw</param>
        public override void Draw(GameTime gameTime)
        {            
            Rectangle targetRect = new Rectangle(x, y, width, height);

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            if (behaviour == ButtonBehaviour.Regular)
            {
                    TacticsGame.Instance.spriteBatch.Draw(bgTexture,
                                                          targetRect, null,
                                                          Color.White,
                                                          0,
                                                          Vector2.Zero,
                                                          SpriteEffects.None,
                                                          0);
            }
            else if (behaviour == ButtonBehaviour.Radio)
            {
                    if (pressed)
                    {
                        TacticsGame.Instance.spriteBatch.Draw(bgTexture,
                                                              targetRect, null,
                                                              Color.Turquoise,
                                                              0,
                                                              Vector2.Zero,
                                                              SpriteEffects.None,
                                                              0);
                    }
                    else
                    {
                        TacticsGame.Instance.spriteBatch.Draw(bgTexture,
                                                              targetRect, null,
                                                              Color.White,
                                                              0,
                                                              Vector2.Zero,
                                                              SpriteEffects.None,
                                                              0);
                    }
            }

            Vector2 size = font.MeasureString(message);
            int xPos = Util.getCentreDrawPointX(x, width, (int)size.X);

            int yPos = Util.getCentreDrawPointY(y, height, (int)size.Y);

            TacticsGame.Instance.spriteBatch.DrawString(font,
                                                        message,
                                                        new Vector2(xPos, yPos),
                                                        Color.Black,
                                                        0.0f,
                                                        new Vector2(0, 0),
                                                        Constants.FontScale,
                                                        SpriteEffects.None,
                                                        0);

            TacticsGame.Instance.spriteBatch.End();


            //Hover Logic
            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            if (Hover)
            {
                TacticsGame.Instance.spriteBatch.Draw(bgTexture,
                                                       targetRect,
                                                       null,
                                                       Color.Red,
                                                       0,
                                                       Vector2.Zero,
                                                       SpriteEffects.None,
                                                       0);
            }

            TacticsGame.Instance.spriteBatch.End();
        }

        /// <summary>
        /// Constructs button object
        /// </summary>
        public ButtonControl()
            : base()
        {

        }
    }
}
